package com.orangelife.mobile.goodsOrder.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.common.jump.PayJumper;
import com.common.util.MoneyUtil2;
import com.common.util.OrangePhoneUtil;
import com.curry.android.http.HttpUtil;
import com.curry.android.util.DecimalUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.IntentHelper;
import com.curry.android.util.JSONHelper;
import com.curry.android.util.MoneyUtil;
import com.curry.android.util.StringUtil;
import com.curry.android.util.ToastHelper;
import com.curry.android.view.ProgressWheel;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.application.ExitApplication;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.common.view.ConfirmDialog;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.goodsOrder.biz.OrderBiz;
import com.orangelife.mobile.goodsOrder.biz.OrderHttp;
import com.orangelife.mobile.goodsOrder.adapter.GoodsOrderListChildrenAdapter;
import com.orangelife.mobile.pay.interfaze.IOuttradenoCallBack;
import com.orangelife.mobile.pay.interfaze.IPayCallBack;
import com.orangelife.mobile.pay.interfaze.IPayManage;
import com.orangelife.mobile.pay.biz.PayManageImpl;
import com.orangelife.mobile.util.DateUtil;
import com.orangelife.mobile.widget.ScrollListView;
import com.orangelife.mobile.widget.imagegridview.ImageGridView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 商品订单详情
 * <p/>
 * Created by YangWei on 2016/1/12.
 *
 * @author yw
 * @since v3.4.0
 */
public class GoodOrdersDetailActivity extends OrangeLifeBaseActivity implements View.OnClickListener {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tvShipName)
    TextView tvShipName;
    @Bind(R.id.tvShipPhone)
    TextView tvShipPhone;
    @Bind(R.id.tvShipAddr)
    TextView tvShipAddr;
    @Bind(R.id.tvShipType)
    TextView tvShipType;
    @Bind(R.id.tvLogisticsCompany)
    TextView tvLogisticsCompany;
    @Bind(R.id.tvLogisticsID)
    TextView tvLogisticsID;
    @Bind(R.id.llOrderDate)
    LinearLayout llOrderDate;
    @Bind(R.id.tvStatus)
    TextView tvStatus;
    @Bind(R.id.tvCommName)
    TextView tvCommName;
    @Bind(R.id.slvGoods)
    ScrollListView slvGoods;
    @Bind(R.id.ll_bottom)
    LinearLayout llBottom;
    @Bind(R.id.rlDeliver)
    LinearLayout rlDeliver;
    @Bind(R.id.tvGoodMoney)
    TextView tvGoodMoney;
    @Bind(R.id.tvGoodPostage)
    TextView tvGoodPostage;
    @Bind(R.id.tvGoodVouchers)
    TextView tvGoodVouchers;
    @Bind(R.id.tvOrderMoney)
    TextView tvOrderMoney;
    @Bind(R.id.tvRefund)
    TextView tvRefund;
    @Bind(R.id.tvOrderID)
    TextView tvOrderID;
    @Bind(R.id.tvOrderTime)
    TextView tvOrderTime;
    @Bind(R.id.tvPayTime)
    TextView tvPayTime;
    @Bind(R.id.tvDeliverTime)
    TextView tvDeliverTime;
    @Bind(R.id.tvRequestRefundTime)
    TextView tvRequestRefundTime;
    @Bind(R.id.tvRefundTime)
    TextView tvRefundTime;
    @Bind(R.id.tvRefuseTime)
    TextView tvRefuseTime;
    @Bind(R.id.tvReturnTime)
    TextView tvReturnTime;
    @Bind(R.id.tvCompleteTime)
    TextView tvCompleteTime;
    @Bind(R.id.btnRefund)
    Button btnRefund;
    @Bind(R.id.btnSubmit)
    Button btnSubmit;
    @Bind(R.id.tvAutomaticReceiptTimes)
    TextView tvAutomaticReceiptTimes;
    @Bind(R.id.tvCancel)
    TextView tvCancel;
    @Bind(R.id.tvRefundTime2)
    TextView tvRefundTime2;
    @Bind(R.id.llCancel)
    LinearLayout llCancel;
    @Bind(R.id.tvRefundInfo)
    TextView tvRefundInfo;
    @Bind(R.id.llRefundTop)
    LinearLayout llRefundTop;
    @Bind(R.id.llButton)
    LinearLayout llButton;
    @Bind(R.id.llPersonInfo)
    LinearLayout llPersonInfo;
    @Bind(R.id.progress)
    ProgressWheel progress;
    @Bind(R.id.gvPhoto)
    ImageGridView gvPhoto;
    @Bind(R.id.view_divider)
    View viewDivider;
    @Bind(R.id.tvContact)
    View tvContact;
    @Bind(R.id.tvPointsPrice)
    TextView tvPointsPrice;
    @Bind(R.id.tvPointsZeng)
    TextView tvPointsZeng;

    private GoodsOrderListChildrenAdapter<Map<String, Object>> myOrderListViewAdapter;
    private List<Map<String, Object>> orderList;
    private Map<String, Object> mapList;
    private Map<String, Object> orderRefundApplication;
    private String orderId;

    private String outtradeno;
    private IPayManage iPayManage;
    private String shipName = "";
    private String shipPhone = "";
    private String shipAddr = "";
    private String shipTypes = "";
    private String totalCost = "";
    private String shipCost = "";
    private String goodsCost = "";
    private String applicationID = "";
    private String couponValue = "";
    private String points = ""; //积分
    private String deduction = ""; //积分抵扣金额
    private int orderStatus;
    private String mViewType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        ButterKnife.bind(this);
        getIntentInfo();
        initView();
        setPageLayout();
        requestOrderDetail();
        requestReadFlag();
    }

    private void getIntentInfo() {
        orderId = getIntent().getStringExtra("orderID");
    }

    private void initView() {
        progress.setVisibility(View.VISIBLE);
        tvTitle.setText(getResources().getString(R.string.order_detail));
        llOrderDate.setVisibility(View.GONE);/*详情界面不需要显示时间*/
        llBottom.setVisibility(View.GONE);
        rlDeliver.setVisibility(View.GONE);
        viewDivider.setVisibility(View.GONE);
    }

    private void setPageLayout() {
        orderList = new ArrayList<>();
        myOrderListViewAdapter = new GoodsOrderListChildrenAdapter<>(
                this, orderList);
        slvGoods.setAdapter(myOrderListViewAdapter);
    }

    private void requestOrderDetail() {
        OrderHttp.getOrderDetail(orderId, handler);
    }

    private void requestReadFlag() {
        OrderHttp.setReadFlag(orderId, handler);//设置已阅读
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @SuppressWarnings("unchecked")
        public void handleMessage(Message msg) {
            String json = String.valueOf(msg.obj);
            JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(json);
            } catch (JSONException e1) {
                e1.printStackTrace();
            }
            switch (msg.what) {
                case OrderBiz.DETAIL_LIST:
                    jsonToList(msg);
                    setData();
                    progress.setVisibility(View.GONE);
                    break;
                case OrderBiz.APILAY_KEY:
                    /** 支付宝key */
                    try {
                        jsonObject.put("outtradeno", outtradeno);
                        jsonObject.put("totalCost", mapList.get("totalCost").toString());
                        callBack(jsonObject);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case OrderBiz.PAID_STATE:
                    mapList = (Map<String, Object>) JSONHelper.jsonToMap(json).get(
                            "entity");
                    setPaidStatus(mapList);
                    break;
                case Constant.WHAT_ERROR_HTTP:
                    ToastHelper.getInstance()._toast(
                            msg.obj.toString());
                    break;
                case Constant.WHAT_ACTIVE_UNEXIST:
                    ToastHelper.getInstance()._toast(msg.obj.toString());
                    break;
                case Constant.WHAT_TOKEN_EXPIRED:
                    isLogin(dialog);
                    break;
                case OrderBiz.CONFIRM_GOOD:
                    Map<String, Object> map = JSONHelper.jsonToMap(json);
                    String errInfo = "";
                    if (map != null) {
                        errInfo = String.valueOf(map.get("errInfo"));
                    }
                    if (errInfo.equals("OK")) {
                        DialogHelper.closeDialog(dialog);
                        ToastHelper.getInstance()._toast(getResources().getString(R.string.submit_success));
                        setResult(RESULT_OK);
                        finish();
                    } else {
                        DialogHelper.closeDialog(dialog);
                        ToastHelper.getInstance()._toast(
                                getResources().getString(
                                        R.string.complaintes_sumbite_fail));
                    }
                    break;
                default:
                    break;
            }
            if (dialog != null)
                DialogHelper.closeDialog(dialog);
        }

        /**
         * 解析数据为List
         *
         * @param msg
         */
        @SuppressWarnings("unchecked")
        private void jsonToList(Message msg) {
            String json = (String) msg.obj;
            mapList = (Map<String, Object>) JSONHelper.jsonToMap(json).get(
                    "entity");
            /** 商品和团购类型 */
            orderList = (List<Map<String, Object>>) mapList
                    .get("goodsInfo");
            myOrderListViewAdapter.setList(orderList);
            myOrderListViewAdapter.notifyDataSetChanged();
        }
    };

    /**
     * 设置详情数据
     *
     * @throws JSONException
     */
    private void setData() {
        orderRefundApplication = (Map<String, Object>) mapList.get("orderRefundApplication");
        mViewType = String.valueOf(mapList.get("viewType"));//积分还是现金
        myOrderListViewAdapter.setViewType(mViewType);
        shipName = String.valueOf(mapList.get("shipName"));
        shipTypes = String.valueOf(mapList.get("shipTypes"));
        shipAddr = String.valueOf(mapList.get("shipAddr"));
        shipPhone = String.valueOf(mapList.get("shipPhone"));

        shipCost = OrderBiz.handleMoney(mapList.get("shipCost"));//物流费用
        totalCost = OrderBiz.handleMoney(mapList.get("totalCost"));//总金额
        couponValue = OrderBiz.handleMoney(mapList.get("couponValue"));//抵用金额
        goodsCost = OrderBiz.handleMoney(mapList.get("goodsCost"));//商品金额
        refundMoney = String.valueOf(mapList.get("refundMoney"));//退款金额
        points = String.valueOf(mapList.get("points"));//赠送的积分
        deduction = String.valueOf(mapList.get("deduction"));//积分抵扣金额
        applicationID = String.valueOf(orderRefundApplication.get("applicationID"));//退款申请ID

        String orderStatus1 = String.valueOf(mapList.get("orderStatus1"));//订单状态
        String merchantName = String.valueOf(mapList.get("merchantName"));//商家名称
        String applyRefundTime = String.valueOf(mapList.get("applyRefundTime"));//申请退款时间
        String payTime = String.valueOf(mapList.get("payTime"));//支付时间

        String automaticReceiptTimes = String.valueOf(mapList.get("automaticReceiptTimes"));//自动收货剩余时间
        String orderTime = String.valueOf(mapList.get("orderTime"));//下单时间
        String deliverGoodsTime = String.valueOf(mapList.get("deliverGoodsTime"));//发货时间
        String throughTime = String.valueOf(orderRefundApplication.get("throughTime"));//同意退款时间
        String applicationReasons = String.valueOf(orderRefundApplication.get("applicationReasons"));//取消订单/退款申请理由
        String applicationType = String.valueOf(orderRefundApplication.get("applicationType"));//(0取消订单,1申请退款)
        String refuseTime = String.valueOf(orderRefundApplication.get("refuseTime"));//申请拒绝时间
        String returnTime = String.valueOf(orderRefundApplication.get("returnTime"));//退货时间
        String completeTime = String.valueOf(mapList.get("completeTime"));//退货时间
        String refundTime = String.valueOf(mapList.get("refundTime"));//退货时间
        String shipType = String.valueOf(mapList.get("shipType"));//配送方式（1邮寄，2到店提货，3送货上门）

        orderStatus = Integer.parseInt(String.valueOf(mapList.get("orderStatus")));//订单状态

        String shipName1 = String.format(getString(R.string.format_ship_name2), shipName);
        tvShipName.setText(shipName1);
        String shipPhone1 = String.format(getString(R.string.format_ship_phone), shipPhone);
        tvShipPhone.setText(shipPhone1);
        String shipAddr1 = String.format(getString(R.string.format_ship_addr), shipAddr);
        tvShipAddr.setText(shipAddr1);
        String shipType1 = String.format(getString(R.string.format_ship_type), shipTypes);
        tvShipType.setText(shipType1);

        if (OrderBiz.isPost(shipTypes) && orderStatus > 1) {/*如果是邮寄，不包括未发货的，显示物流信息*/
            tvLogisticsCompany.setVisibility(View.VISIBLE);
            tvLogisticsID.setVisibility(View.VISIBLE);
            OrderBiz.setTextInfo(tvLogisticsCompany, getString(R.string.format_logistics_company), String.valueOf(mapList.get("logisticsCompany")));
            OrderBiz.setTextInfo(tvLogisticsID, getString(R.string.format_logistics_id), String.valueOf(mapList.get("logisticsID")));
        } else {
            tvLogisticsCompany.setVisibility(View.GONE);
            tvLogisticsID.setVisibility(View.GONE);
        }
        //提示自动收货时间
        if (!(orderStatus == 2 || orderStatus == 10) || automaticReceiptTimes.equals("0") || automaticReceiptTimes.equals("")) {
            tvAutomaticReceiptTimes.setVisibility(View.GONE);
        } else {
            tvAutomaticReceiptTimes.setVisibility(View.VISIBLE);
            tvAutomaticReceiptTimes.setText("还剩" + DateUtil.setDays(Long.parseLong(automaticReceiptTimes)) + "后自动确认收货");
        }

        OrderBiz.setTextInfo(tvPayTime, getString(R.string.format_pay_time), OrderBiz.timeStampToDate2(payTime));
        OrderBiz.setTextInfo(tvDeliverTime, getString(R.string.format_deliver_time), OrderBiz.timeStampToDate2(deliverGoodsTime));
        OrderBiz.setTextInfo(tvOrderTime, getString(R.string.format_order_time), OrderBiz.timeStampToDate2(orderTime));
        OrderBiz.setTextInfo(tvReturnTime, getString(R.string.format_return_time), OrderBiz.timeStampToDate2(returnTime));
        OrderBiz.setTextInfo(tvCompleteTime, getString(R.string.format_complete_time), OrderBiz.timeStampToDate2(completeTime));
        OrderBiz.setTextInfo(tvRefundTime2, getString(R.string.format_cancel_time), OrderBiz.timeStampToDate2(refundTime));

        String time_fomat1 = getString(R.string.format_request_cancel_time);
        String time_fomat3 = getString(R.string.format_refuce_cancel_time);

        if (!StringUtil.isBlank(applicationType)) {
            if (applicationType.equals("1")) {
                time_fomat1 = getString(R.string.format_request_refund_time);
                String time_fomat2 = getString(R.string.format_refund_time);
                time_fomat3 = getString(R.string.format_refuce_refund_time);
                OrderBiz.setTextInfo(tvRefundTime, time_fomat2, OrderBiz.timeStampToDate2(throughTime));
            }
        }
        OrderBiz.setTextInfo(tvRequestRefundTime, time_fomat1, OrderBiz.timeStampToDate2(applyRefundTime));
        OrderBiz.setTextInfo(tvRefuseTime, time_fomat3, OrderBiz.timeStampToDate2(refuseTime));

        OrderBiz.setTextInfo(tvGoodMoney, "商品总金额：" + Constant.RMB_FLAG + getString(R.string.format_total_amount), goodsCost);
        if (shipType.equals("1")) {
            OrderBiz.setTextInfo(tvGoodPostage, "邮费：+ " + Constant.RMB_FLAG + getString(R.string.format_postage), shipCost);
        } else if (shipType.equals("3")) {
            OrderBiz.setTextInfo(tvGoodPostage, "配送费：+ " + Constant.RMB_FLAG + getString(R.string.format_postage2), shipCost);
        } else {
            tvGoodPostage.setVisibility(View.GONE);
        }
        OrderBiz.setTextInfo(tvGoodVouchers, "抵用券：- " + Constant.RMB_FLAG + getString(R.string.format_vouchers), couponValue);
        OrderBiz.setTextInfo(tvOrderMoney, "订单总金额：" + Constant.RMB_FLAG + getString(R.string.format_order_amount), totalCost);
        OrderBiz.setTextInfo(tvPointsPrice, "橙汁：- " + Constant.RMB_FLAG + getString(R.string.format_points), MoneyUtil.handleMoney(deduction));
        OrderBiz.setTextInfo(tvPointsZeng, getString(R.string.format_zeng_points), points);
        OrderBiz.setTextInfo(tvOrderID, getString(R.string.format_order_id), orderId);

        if (StringUtil.isBlank(refundMoney)) {
            tvRefund.setVisibility(View.GONE);
        } else {
            tvRefund.setVisibility(View.VISIBLE);
            tvOrderMoney.setTextColor(getResources().getColor(R.color.text_grey));
            refundMoney = OrderBiz.handleMoney(refundMoney);
            OrderBiz.setTextInfo(tvRefund, "退款金额：" + Constant.RMB_FLAG + getString(R.string.format_refund), refundMoney);
        }
        tvCommName.setText(merchantName);
        tvStatus.setText(orderStatus1);
        //--'订单状态(0待付款；1待卖家发货；2卖家已发货；3交易完成;4已取消；
        // 5支付中;6取消订单中；7受理中；8已退款;9拒绝取消;10拒绝退款;
        // 11同意退款;12待卖家收货(即买家退回商品);13卖家已收货)'
        if (orderStatus == 0) {
            btnRefund.setVisibility(View.GONE);
            btnSubmit.setVisibility(View.VISIBLE);
            btnSubmit.setText("确认支付");
            if (!"已超时".equals(orderStatus1)) {
                llButton.setVisibility(View.VISIBLE);
            } else {
                llButton.setVisibility(View.GONE);
            }
        } else if (orderStatus == 1) {
            btnRefund.setVisibility(View.GONE);
            btnSubmit.setVisibility(View.VISIBLE);
            btnSubmit.setText("取消订单");
        } else if (orderStatus == 2) {
            btnRefund.setVisibility(View.VISIBLE);
            btnSubmit.setVisibility(View.VISIBLE);
            btnSubmit.setText("确认收货");
        } else if (orderStatus == 10) {
            btnRefund.setVisibility(View.GONE);
            btnSubmit.setVisibility(View.VISIBLE);
            btnSubmit.setText("确认收货");
        } else if (orderStatus == 11) {
            btnRefund.setVisibility(View.GONE);
            btnSubmit.setVisibility(View.VISIBLE);
            btnSubmit.setText("退回商品");
        } else {
            btnRefund.setVisibility(View.GONE);
            btnSubmit.setVisibility(View.GONE);
        }
        //取消订单与申请退款 详情页展示
        Integer[] cancel = {7, 11, 12, 8};
        if ((orderStatus == 6 || orderStatus == 8) && applicationType.equals("0")) {
            llPersonInfo.setVisibility(View.GONE);
            llCancel.setVisibility(View.VISIBLE);
            tvCancel.setText(applicationReasons);
        } else if (Arrays.asList(cancel).contains(orderStatus) && applicationType.equals("1")) {
            llPersonInfo.setVisibility(View.GONE);
            llRefundTop.setVisibility(View.VISIBLE);
            tvRefundInfo.setText(applicationReasons);
            String pics = orderRefundApplication.get("photo").toString();
            gvPhoto.init(this, pics, "##");
        }
        btnSubmit.setOnClickListener(this);
        btnRefund.setOnClickListener(this);
        tvContact.setOnClickListener(this);

        setPointView();
    }


    /** viewType=1金钱，viewType=2，积分 */
    private void setPointView() {
        if ("2".equals(mViewType)) {
            tvGoodMoney.setVisibility(View.GONE);
            tvGoodPostage.setVisibility(View.GONE);
            tvGoodVouchers.setVisibility(View.GONE);
            tvPointsPrice.setVisibility(View.GONE);
            btnRefund.setVisibility(View.GONE);
            btnSubmit.setVisibility(View.GONE);
            tvPointsZeng.setVisibility(View.GONE);
            totalCost = MoneyUtil2.trimZero(totalCost);
            tvOrderMoney.setText("订单总金额：" + totalCost + "ml橙汁");
        }
    }

    private String refundMoney;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnSubmit:
                if (orderStatus == 0) {
                    paid();
                } else if (orderStatus == 1) {
                    refundMoney = totalCost;
                    haveCouponValue(0);
                } else if (orderStatus == 2 || orderStatus == 10) {
                    confirm();
                } else if (orderStatus == 11) {
                    OrderBiz.backGoods(applicationID, GoodOrdersDetailActivity.this);
                }
                break;
            case R.id.btnRefund:
                refundMoney = DecimalUtil.subtract(totalCost, shipCost);
                haveCouponValue(1);
                break;
            case R.id.tvContact:
                OrangePhoneUtil.callCustomerService2(GoodOrdersDetailActivity.this);
                break;
        }
    }

    /**
     * 确认收货
     */
    private void confirm() {

        final ConfirmDialog dialog = new ConfirmDialog(this, "", "确认收货后将不再支持退款申请，\n" +
                "是否确认收货？", "取消", "确认");
        dialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
            @Override
            public void doLeft() {
                dialog.dismiss();
            }

            @Override
            public void doRight() {
                dialog.dismiss();
                showDialog();
                OrderHttp.postConfirmRequest(orderId, handler);
            }
        });
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    /**
     * 使用抵用券时的提示
     */
    private void haveCouponValue(final int type) {
        String message = getResources().getString(R.string.cancel_tip1);
        if (type == 1) {
            message = getResources().getString(R.string.cancel_tip2);
        }
        if (Double.parseDouble(couponValue) > 0) {
            final ConfirmDialog dialog = new ConfirmDialog(this, "", message, "取消", "确认");
            dialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                @Override
                public void doLeft() {
                    dialog.dismiss();
                }

                @Override
                public void doRight() {
                    dialog.dismiss();
                    jumpToCancel(type);
                }
            });
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();
        } else {
            jumpToCancel(type);
        }
    }

    /**
     * 取消订单/申请退款
     *
     * @param type
     */
    private void jumpToCancel(int type) {
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        bundle.putString("orderID", orderId);
        bundle.putString("refundMoney", refundMoney);
        IntentHelper.getInstances().bundleWithBundleForResult(this, CancelOrderActivity.class, bundle, OrderBiz.CANCEL_JUMP);
    }


    private void paid() {
        iPayManage = new PayManageImpl(new IOuttradenoCallBack() {
            @Override
            public void data(JSONObject jsonObject) {
                ExitApplication.getInstance().addPaidActivity(GoodOrdersDetailActivity.this);
                String orderType = null;
                try {
                    Map<String, Object> entity = JSONHelper.jsonToMap(jsonObject.get("entity").toString());
                    outtradeno = String.valueOf(entity.get("outtradeno"));
                    points = String.valueOf(entity.get("points"));
                    orderType = String.valueOf(entity.get("orderType"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String totalCost = String.valueOf(mapList.get("totalCost"));
                String orderContent = String.valueOf(mapList.get("merchantName"));
                String paidType = OrderBiz.getPaidTypeFromOrderType(String.valueOf(mapList.get("orderType")));
                ArrayList<String> listProducts = new ArrayList<>();
                for (int i = 0; i < orderList.size(); i++) {
                    listProducts.add(orderList.get(i).get("productID").toString());
                }
                PayJumper.jumpToPayFromGoodOrderDetail(GoodOrdersDetailActivity.this, orderId, paidType, totalCost, orderContent, outtradeno, points, couponValue, listProducts, orderType);
            }
        });
        iPayManage.getOuttradenoFromServer(mapList.get("orderID").toString(), -1);
    }

    @SuppressWarnings("deprecation")
    private void getApilayPaidStatus() {
        Map<String, Object> rootMap = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        map.put("outTradeNo", outtradeno);
        rootMap.put("map", map);
        rootMap.put("url", Constant.URL_PAID_STATUS);
        rootMap.put("wat", OrderBiz.PAID_STATE);
        new JSONRequest(JSONRequest.GET, rootMap, handler, HttpUtil.ACTION_URI);
    }

    private void setPaidStatus(Map<String, Object> mapList) {
        if (mapList.get("tradestatus").equals("2")) {
//            tvPaidStatus.setText(getResources().getString(R.string.paided));
            tvStatus.setText(mapList.get("orderStatus1").toString());
        } else if (mapList.get("tradestatus").equals("3")) {
//            tvPaidStatus.setText(getResources().getString(R.string.paiding));
            tvStatus.setText(mapList.get("orderStatus1").toString());
        }
    }

    /**
     * 设置支付完成后的支付按钮颜色
     */
    private void setPaidColor() {
        btnSubmit.setEnabled(false);
        btnSubmit.setBackgroundResource(R.drawable.shape_btn_gray);
    }

    private void callBack(JSONObject jsonObject) {
        iPayManage = new PayManageImpl(new IPayCallBack() {
            @Override
            public void success() {
                Toast.makeText(GoodOrdersDetailActivity.this, "支付成功",
                        Toast.LENGTH_SHORT).show();
                getApilayPaidStatus();
                setPaidColor();
            }

            @Override
            public void handling() {
                Toast.makeText(GoodOrdersDetailActivity.this, "支付结果确认中",
                        Toast.LENGTH_SHORT).show();
                // 支付中的状态
                getApilayPaidStatus();
                setPaidColor();
            }

            @Override
            public void fail() {
                Toast.makeText(GoodOrdersDetailActivity.this, "支付失败",
                        Toast.LENGTH_SHORT).show();
            }
        });
        iPayManage.setParameter(GoodOrdersDetailActivity.this, jsonObject);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == OrderBiz.CANCEL_JUMP && resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            finish();
        }
        if (requestCode == 333) {
            setResult(RESULT_OK);
            finish();
        }
    }


}
